home *** CD-ROM | disk | FTP | other *** search
- //____________________________________________________________
- // MoreSndCommands.c
- //
- // Copyright © Dan Parks Sydow, 1995
- // From the book:
- // "Graphics and Sound Programming Techniques for the Mac",
- // M&T Books, 1995
-
-
- //____________________________________________________________
-
- #include <Sound.h>
-
-
- //____________________________________________________________
-
- pascal void SoundChannelCallback( SndChannelPtr, SndCommand );
- OSErr InstallCallbackCommand( SndChannelPtr );
- void CleanUpSoundIfFinished( void );
- void StopSoundPlaying( void );
- void PlaySoundResourceAsynch( short );
- void AnimateWhileSoundPlays( void );
- OSErr SetSoundAmplitude( SndChannelPtr, short );
- OSErr SetSoundRate( SndChannelPtr, long );
- void InitializeToolbox( void );
- void OpenDisplayWindow( void );
- void LoadAndSetupPicture( void );
- void MovePictureOnePixel( void );
- SndChannelPtr OpenOneAsynchSoundChannel( void );
- OSErr DisposeOneSoundChannel( SndChannelPtr );
-
-
- //____________________________________________________________
-
- #define rMusicSound 9000
- #define rDisplayWindow 128
- #define rBearPicture 128
-
- #define k11kHzFreqRate 0x00008000
- #define k22kHzFreqRate 0x00010000
- #define k44kHzFreqRate 0x00020000
- #define k66kHzFreqRate 0x00030000
-
-
- //____________________________________________________________
-
- short gSoundPlaying = false;
- Boolean gCallbackExecuted = false;
- SndChannelPtr gSoundChannel = nil;
- Handle gSoundHandle = nil;
- Boolean gDone = false;
- PicHandle gThePicture;
- Rect gTheRect;
-
-
- //____________________________________________________________
-
- void main( void )
- {
- NumVersion theSndMgrVers;
- EventRecord theEvent;
-
- InitializeToolbox();
-
- theSndMgrVers = SndSoundManagerVersion();
- if ( theSndMgrVers.majorRev < 3 )
- ExitToShell();
-
- OpenDisplayWindow();
- LoadAndSetupPicture();
-
- while ( gDone == false )
- {
- WaitNextEvent( everyEvent, &theEvent, 15L, nil );
-
- switch ( theEvent.what )
- {
- case mouseDown:
- gDone = true;
- break;
-
- case keyDown:
- PlaySoundResourceAsynch( rMusicSound );
- break;
- }
- }
- }
-
-
- //____________________________________________________________
-
- OSErr SetSoundRate( SndChannelPtr theChannel, long theRate )
- {
- SndCommand theCommand;
- OSErr theError;
-
- theCommand.cmd = rateCmd;
- theCommand.param1 = 0;
- theCommand.param2 = theRate;
-
- theError = SndDoImmediate( theChannel, &theCommand );
-
- return ( theError );
- }
-
-
- //____________________________________________________________
-
- OSErr SetSoundAmplitude( SndChannelPtr theChannel, short theAmp )
- {
- SndCommand theCommand;
- OSErr theError;
-
- theCommand.cmd = ampCmd;
- theCommand.param1 = theAmp;
- theCommand.param2 = 0;
-
- theError = SndDoImmediate( theChannel, &theCommand );
-
- return ( theError );
- }
-
-
- //____________________________________________________________
-
- void PlaySoundResourceAsynch( short theResID )
- {
- OSErr theError;
-
- if ( gSoundChannel != nil )
- StopSoundPlaying();
-
- gSoundChannel = OpenOneAsynchSoundChannel();
- if ( gSoundChannel == nil )
- ExitToShell();
-
- gSoundHandle = GetResource( 'snd ', theResID );
- if ( gSoundHandle == nil )
- ExitToShell();
-
- DetachResource( gSoundHandle );
- HLock( gSoundHandle );
-
- gSoundPlaying = true;
-
- theError = SndPlay( gSoundChannel, (SndListHandle)gSoundHandle, true );
-
- if ( theError == noErr )
- theError = InstallCallbackCommand( gSoundChannel );
- else
- ExitToShell();
-
- AnimateWhileSoundPlays();
- }
-
-
- //____________________________________________________________
-
- void AnimateWhileSoundPlays( void )
- {
- EventRecord theEvt;
- Boolean loopDone = false;
- char theChar;
- OSErr theError;
- short theAmplitude = 255;
-
- while ( loopDone == false )
- {
- CleanUpSoundIfFinished();
-
- if ( gSoundPlaying == true )
- MovePictureOnePixel();
- else
- loopDone = true;
-
- WaitNextEvent( everyEvent, &theEvt, 15L, nil );
-
- switch ( theEvt.what )
- {
- case keyDown:
- theChar = theEvt.message & charCodeMask;
- switch ( theChar )
- {
- case '+':
- if ( theAmplitude <= 225 )
- theAmplitude += 30;
- theError = SetSoundAmplitude( gSoundChannel, theAmplitude );
- if ( theError != noErr )
- ExitToShell();
- break;
- case '-':
- if ( theAmplitude >= 30 )
- theAmplitude -= 30;
- theError = SetSoundAmplitude( gSoundChannel, theAmplitude );
- if ( theError != noErr )
- ExitToShell();
- break;
- case 'p':
- theError = SetSoundRate( gSoundChannel, k22kHzFreqRate );
- if ( theError != noErr )
- ExitToShell();
- break;
- case 's':
- theError = SetSoundRate( gSoundChannel, k11kHzFreqRate );
- if ( theError != noErr )
- ExitToShell();
- break;
- case 'f':
- theError = SetSoundRate( gSoundChannel, k66kHzFreqRate );
- if ( theError != noErr )
- ExitToShell();
- break;
- default:
- StopSoundPlaying();
- loopDone = true;
- break;
- }
- break;
- }
- }
- }
-
-
- //____________________________________________________________
-
- void StopSoundPlaying( void )
- {
- if ( gSoundChannel != nil )
- {
- gCallbackExecuted = true;
- gSoundPlaying = false;
- }
- CleanUpSoundIfFinished();
- }
-
-
- //____________________________________________________________
-
- void CleanUpSoundIfFinished( void )
- {
- OSErr theError;
-
- if ( gCallbackExecuted == true )
- {
- HUnlock( gSoundHandle );
- ReleaseResource( gSoundHandle );
- gSoundHandle = nil;
-
- theError = DisposeOneSoundChannel( gSoundChannel );
- if ( theError != noErr )
- ExitToShell();
-
- gSoundChannel = nil;
- gCallbackExecuted = false;
- }
- }
-
-
- //____________________________________________________________
-
- OSErr InstallCallbackCommand( SndChannelPtr theChannel )
- {
- OSErr theError;
- SndCommand theCommand;
-
- theCommand.cmd = callBackCmd;
- theCommand.param1 = 0;
- #ifndef powerc
- theCommand.param2 = SetCurrentA5();
- #else
- theCommand.param2 = 0;
- #endif
-
- theError = SndDoCommand( theChannel, &theCommand, true );
-
- return ( theError );
- }
-
-
- //____________________________________________________________
-
- pascal void SoundChannelCallback( SndChannelPtr theChannel, SndCommand theCommand )
- {
- long theA5;
-
- #ifndef powerc
- theA5 = SetA5( theCommand.param2 );
- #endif
-
- gCallbackExecuted = true;
- gSoundPlaying = false;
-
- #ifndef powerc
- theA5 = SetA5( theA5 );
- #endif
- }
-
-
-
- //____________________________________________________________
-
- SndChannelPtr OpenOneAsynchSoundChannel( void )
- {
- SndChannelPtr theChannel;
- OSErr theError;
- SndCallBackUPP theCallBackUPP;
-
- theCallBackUPP = NewSndCallBackProc( SoundChannelCallback );
-
- theChannel = nil;
- theError = SndNewChannel( &theChannel, 0, 0, theCallBackUPP );
-
- if ( theError != noErr )
- {
- DisposePtr( (Ptr)theChannel );
- theChannel = nil;
- }
-
- return ( theChannel );
- }
-
-
- //____________________________________________________________
-
- OSErr DisposeOneSoundChannel( SndChannelPtr theChannel )
- {
- OSErr theError;
-
- theError = SndDisposeChannel( theChannel, true );
- DisposePtr( (Ptr)theChannel );
-
- return ( theError );
- }
-
-
- //____________________________________________________________
-
- void InitializeToolbox( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- FlushEvents( everyEvent, 0 );
- InitCursor();
- }
-
-
- //____________________________________________________________
-
- void OpenDisplayWindow( void )
- {
- WindowPtr theWindow;
-
- theWindow = GetNewWindow( rDisplayWindow, nil, (WindowPtr)-1L );
- ShowWindow( theWindow );
- SetPort( theWindow );
- }
-
-
- //____________________________________________________________
-
- void LoadAndSetupPicture( void )
- {
- short theWidth;
- short theHeight;
- short left = 475;
- short top = 10;
-
- gThePicture = GetPicture( rBearPicture );
- gTheRect = (**gThePicture).picFrame;
- theWidth = gTheRect.right - gTheRect.left;
- theHeight = gTheRect.bottom - gTheRect.top;
- SetRect( &gTheRect, left, top, left + theWidth, top + theHeight );
- }
-
-
- //____________________________________________________________
-
- void MovePictureOnePixel( void )
- {
- OffsetRect( &gTheRect, -1, 0 );
- DrawPicture( gThePicture, &gTheRect );
- }
-